import grpc
import AccountRefer_pb2
import AccountRefer_pb2_grpc


def generate_id_query(ids):
    # 用yield发送流数据
    for i in ids:
        yield AccountRefer_pb2.IdQuery(id=i)


class AccountClientRefer:

    def __init__(self, target):
        self.channel = grpc.insecure_channel(target)
        self.stub = AccountRefer_pb2_grpc.AccountServiceStub(self.channel)

    '''
    通过id查到账户
    '''

    def findOneById(self, id_in):
        id_query = AccountRefer_pb2.IdQuery(id=id_in)
        response = self.stub.getOne(id_query)
        return response

    '''
    通过id范围查到账户集合
    '''

    def findAllByRange(self, lower_in, upper_in):
        range_query = AccountRefer_pb2.IdRangeQuery(lower=lower_in, upper=upper_in)
        response = self.stub.getByRange(range_query)
        return response

    '''
    通过id集合查到账户集合
    '''

    def findAllByIds(self, ids):
        return self.stub.getMany(generate_id_query(ids))

    '''
    通过id集合获取账号余额总数
    '''

    def getBalanceSumByIds(self, ids):
        return self.stub.calBalanceSum(generate_id_query(ids))
